package com.geen.tuoke.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import com.geen.tuoke.base.BaseViewModel
import com.geen.tuoke.bean.CustomerInfo
import com.geen.tuoke.bean.request.UpdatePhoneTaskRequest
import com.geen.tuoke.network.api.apiService
import com.geen.tuoke.utils.ext.request
import com.geen.tuoke.utils.ext.requestNoCheck
import com.geen.tuoke.utils.ext.toastShort

/**
 *
 * @Description: 拨打网络电话
 * @Author: geen
 * @CreateDate: 2022/1/22 23:30
 * @Version: 1.0
 */
class PhoneNumberVm : BaseViewModel() {

    var customerInfo = MutableLiveData<CustomerInfo?>()
    var mobileNumber = MutableLiveData<String>()
    val callOutSuccess = MutableLiveData<Boolean>()
    var showCallResultMsg = MutableLiveData<String>()
    //0 未接通 1未接通    -1 空号
    var callOutResult = MutableLiveData<Int>().apply {
        value = 0
    }

    fun callOut() {
        if (mobileNumber.value.isNullOrEmpty()) {
            return
        }
        requestNoCheck({ apiService.callout(mobile = mobileNumber.value.toString().trim()) }, {
            val result = if (it.data == "0000") {
                1
            } else if (it.data == "1002" || it.data == "1003") {
                -1
            } else {
                0
            }
            callOutResult.value = result

            if (it.data.equals("0000")) {
                callOutSuccess.value = true
            }else{
                showCallResultMsg.value = it.msg
            }
        }, {
            callOutSuccess.value = false
            toastShort(it.errorMsg)
        }, true)
    }

    fun updatePhoneTask(updatePhoneTaskRequest: UpdatePhoneTaskRequest){
        request({ apiService.updatePhoneTask(updatePhoneTaskRequest)},{
            toastShort("更新成功")
        },{
            toastShort(it.errorMsg)
        },false)
    }

    fun getCustomerInfoByMobile(){
        if(mobileNumber.value.isNullOrEmpty()){
            return
        }
        request({ apiService.getCustomInfoByMobile(mobileNumber.value.toString())},{
            customerInfo.value = it
        },{
            customerInfo.value = null
        })
    }

}